package org.zuel.Luce.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;


import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;



@Configuration
@MapperScan(basePackages = {"org.zuel.luce.dao.secDao"},sqlSessionFactoryRef = "luceSecSqlSessionFactory")
public class LuceSecDataSourceConfig {

    @Bean(name = "secDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secend")
    public DataSource secDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "luceSecSqlSessionFactory")
    public SqlSessionFactory secSqlSessionFactory(@Qualifier("secDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:org/zuel/Luce/dao/secDao/*.xml"));//第一个mapper.xml
        return sessionFactoryBean.getObject();
    }

    @Bean(name = "secSqlSessionTemplate")
    public SqlSessionTemplate secSqlSessionFactoryTemplate(@Qualifier("luceSecSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
